*********************************************************************************************************************
* This replication file calculates the change in the number of notaries after a counterfactual change in regulation *
*********************************************************************************************************************
* 1. Collect entry levels across all districts under the counterfactual of welfare-maximization with an optimal price reduction.
* 2. Collect entry levels across all districts under the counterfactual of free entry with an optimal price reduction.
* 3. Merge the information on changes in notary levels.
* 4. Load the GIS information on Belgian municipalities.
* 5. Calculate and plot the change in notaries, replicating Figure A2.


***********************
* 0 Set path to files *
***********************
clear all
capture log close
set more off
set trace off
cd $main_directory


**********************************************************************************************************************************
* 1. Collect entry levels across all districts under the counterfactual of welfare-maximization with an optimal price reduction. *
**********************************************************************************************************************************

global districts 1 2 3 4 5 6 8 9 10 11 12 //Select the district ids (1 to 12, without 7)

//Generate the file with the entry levels with welfare maximizing entry and a 17% price decrease for empty locations
//Welfare maximization
global markup_change_welfare 17
foreach district in $districts {
local file_name "Estimates\Entry_counterfactual\welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_`district'_0"
//Load the results data
if `district'==1 {
use "`file_name'.dta", replace
}
else{
append using "`file_name'.dta"
} //End of if-condition distinguishing between the first dataset and all others
} //End of loop going over districts
//Save the data
save "Estimates\Graphs\notaries_welfare_`=$markup_change_welfare'pct_0.dta", replace

************************************************************************************************************************
* 2. Collect entry levels across all districts under the counterfactual of free entry with an optimal price reduction. *
************************************************************************************************************************
//Generate a file with the entry levels with free entry and a 19% price decrease for empty locations (the optimal price reduction)
global markup_change_free 19 //Select the optimal price reduction level under free entry
foreach district in $districts {
local file_name "Estimates\Entry_counterfactual\free_`=$markup_change_free'_`=$markup_change_free'_`district'_0" //Load the results from each district
//Load the results data
if `district'==1 {
use "`file_name'.dta", replace
}
else{
append using "`file_name'.dta"
} //End of if-condition distinguishing between the first dataset and all others
} //End of loop going over districts
//Save the data
save "Estimates\Graphs\notaries_free_`=$markup_change_free'pct_0.dta", replace

*********************************************************
* 3. Merge the information on changes in notary levels. *
*********************************************************

//Load the entry levels under an optimal distribution of firms
use "Data_Belgium\Generated_data\baseline_demand_dataset.dta", clear //Load the baseline dataset of notaries
append using "Data_Belgium\Generated_data\baseline_dataset_counter_post_offices.dta" //Load also the postoffice information, since we will allow for entry in new locations.
keep if common==1 //Keep only one observation per office
//Merge with the results
merge m:1 bvdid_num using "Estimates\Graphs\notaries_free_`=$markup_change_free'pct_0.dta"
replace NotPerOff_free_`=$markup_change_free'_`=$markup_change_free'_0=0 if NotPerOff_free_`=$markup_change_free'_`=$markup_change_free'_0==.
drop _merge
merge m:1 bvdid_num using "Estimates\Graphs\notaries_welfare_`=$markup_change_welfare'pct_0.dta"
replace NotPerOff_welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_0=0 if NotPerOff_welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_0==.
drop _merge
//Calculate the sum of entrants per municipality under the status quo and the counterfactuals
collapse (mean) population (sum) NotPerOff NotPerOff_free_`=$markup_change_free'_`=$markup_change_free'_0 NotPerOff_welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_0, by(nis)
//Save the results
save "Estimates\Graphs\notary_entry_summary.dta", replace

**********************************************************
* 4. Load the GIS information on Belgian municipalities. *
**********************************************************

clear
//Load the GIS data on European census units
use "Data_Belgium\GIS_data_census_units\units_db.dta", clear
//Generate a country identifier
gen country=substr(CENSUS_ID,1,2)
//Keep only the observations in Belgium
keep if country=="BE"
//Generate the NIS variable
gen nis=substr(CENSUS_ID,7,5)
destring nis, replace
//Merge with data on relevant notary-level variable
merge 1:1 nis using "Estimates\Graphs\notary_entry_summary.dta"
drop _merge

************************************************************************
* 5. Calculate and plot the change in notaries, replicating Figure A2. *
************************************************************************

gen abs_change_welfare=NotPerOff_welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_0-NotPerOff
su abs_change_welfare, de
gen relative_change_welfare=abs_change_welfare/NotPerOff
su relative_change_welfare, de 
gen max_relative_change_welfare=r(max)
replace relative_change_welfare=max_relative_change_welfare if NotPerOff==0 & NotPerOff_welfare_`=$markup_change_welfare'_`=$markup_change_welfare'_0>0
gen abs_change_free=NotPerOff_free_`=$markup_change_free'_`=$markup_change_free'_0-NotPerOff
su abs_change_free, de
gen relative_change_free=abs_change_free/NotPerOff
su relative_change_free
gen max_relative_change_free=r(max)
replace relative_change_free=max_relative_change_free if NotPerOff==0 & NotPerOff_free_`=$markup_change_free'_`=$markup_change_free'_0>0

su relative_change_free, de 
colorpalette Greys, n(7) nograph
local colors `r(p)'
spmap relative_change_welfare using Data_Belgium\GIS_data_census_units\geo2xy_census_coor_proj, id(id) osize(vthin) fcolor("`colors'") clmethod(custom) clbreaks(-1 -0.01 0.01 1 2 3 4 20) legend(label(1 "remaining uncovered") label(2 "[-1,0)") label(3 "no change") label(4 "(0,1]")  label(8 "4+ or newly covered")) legend(size(medium))
graph save Estimates\Graphs\relative_change_welfare, replace
graph export "Estimates\Graphs\figure_A2_welfare.eps", replace

spmap relative_change_free using Data_Belgium\GIS_data_census_units\geo2xy_census_coor_proj, id(id) osize(vthin) fcolor("`colors'") clmethod(custom) clbreaks(-1 -0.01 0.01 1 2 3 4 20) legend(off)
graph save Estimates\Graphs\relative_change_free, replace
graph export "Estimates\Graphs\figure_A2_free.eps", replace
